package com.sx.pipeline.ui.supply.approve.frag

import android.os.Bundle
import android.text.TextUtils
import android.view.View
import androidx.annotation.NonNull
import androidx.recyclerview.widget.LinearLayoutManager
import com.blankj.utilcode.util.ToastUtils
import com.lxj.xpopup.XPopup
import com.scwang.smart.refresh.layout.api.RefreshLayout
import com.scwang.smart.refresh.layout.listener.OnRefreshLoadMoreListener
import com.sx.pipeline.R
import com.sx.pipeline.app.base.CYDataBindingBaseFragment
import com.sx.pipeline.app.config.IntentKey
import com.sx.pipeline.arouth.RouteManager
import com.sx.pipeline.databinding.FragParticipateTaskBinding
import com.sx.pipeline.repos.http.res.ReleaseSupplyVo
import com.sx.pipeline.ui.supply.approve.ApprovalSupplyVM


/**
 * @Author: YY
 * @Date: 2022/4/13
 * @Description:
 */
class ApprovalSupplyFragment :
    CYDataBindingBaseFragment<FragParticipateTaskBinding, ApprovalSupplyVM>(R.layout.frag_participate_task) {
    private lateinit var mAdapter: ApprovalSupplyListAdapter
    private var status = 0 //任务状态


    companion object {
        fun newInstance(status: Int) =
            ApprovalSupplyFragment().apply {
                arguments = Bundle().apply {
                    putInt(IntentKey.STATUS, status)
                }
            }
    }

    override fun initData() {
        status = arguments?.getInt(IntentKey.STATUS)!!
        mViewModel.auditSupplyList(status)
    }


    private fun initRv() {
        if (!this::mAdapter.isInitialized) {
            mAdapter = ApprovalSupplyListAdapter()
            mBinding.recyclerView.layoutManager =
                LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false)
        }
        mAdapter.setOnItemClickListener { adapter, view, position ->
            var info = adapter.data[position] as ReleaseSupplyVo
            RouteManager.Mine.intentToSupplyApprovalDetail(info.id)
        }
        mBinding.recyclerView.adapter = mAdapter

        //拒绝任务
        mAdapter.rejectRequireCallback = { id: Int ->
            XPopup.Builder(context)
                .autoOpenSoftInput(true)
                .isDestroyOnDismiss(true) //对于只使用一次的弹窗，推荐设置这个
                .asInputConfirm(
                    getString(R.string.txt_please_input_reject_reason),
                    null,
                    "",
                    getString(R.string.txt_please_input),
                    {
                        if (TextUtils.isEmpty(it)) {
                            ToastUtils.showShort("请输入拒绝原因")
                            return@asInputConfirm
                        }
                        mViewModel.getSupplyAudit(id,it,2)
                    },
                    null,
                    R.layout.dialog_task_reject
                ) //最后一个参数绑定已有布局
                .show()
        }
        //同意任务
        mAdapter.agreeRequireCallback = {id: Int->
            mViewModel.getSupplyAudit(id,"",1)
        }
    }

    override fun initViewObservable() {
        super.initViewObservable()
        mViewModel.approvalSupplyListEvent.observe(this) {
            initRv()

            val taskList = it as List<ReleaseSupplyVo>
            if (mViewModel.page == 1) {
                if (taskList.isNotEmpty()) {
                    mAdapter?.data = taskList as MutableList<ReleaseSupplyVo>
                    mBinding.recyclerView.visibility = View.VISIBLE
                    mBinding.emptyView.visibility = View.GONE
                } else {
                    mBinding.recyclerView.visibility = View.GONE
                    mBinding.emptyView.visibility = View.VISIBLE
                }
                mBinding.refreshLayout.finishRefresh()
            } else {
                if (taskList.isNotEmpty()) {
                    mAdapter?.addData(taskList)
                    mBinding.refreshLayout.finishLoadMore()
                } else {
                    mBinding.refreshLayout.finishLoadMoreWithNoMoreData()
                }
            }
        }

        //审批成功/拒绝成功
        observe(mViewModel.auditSuccessEvent){
            mViewModel.page = 1
            mViewModel.auditSupplyList(status)
        }
    }


    override fun initListener() {
        super.initListener()
        mBinding?.refreshLayout?.setOnRefreshLoadMoreListener(object : OnRefreshLoadMoreListener {
            override fun onLoadMore(@NonNull refreshLayout: RefreshLayout) {
                mViewModel.page += 1
                mViewModel.auditSupplyList(status)
            }

            override fun onRefresh(@NonNull refreshLayout: RefreshLayout) {
                mViewModel.page = 1
                mViewModel.auditSupplyList(status)
            }
        })
    }


}